package com.blt.govdata.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.blt.govdata.controller.base.BaseController;
import com.blt.govdata.model.TSysUserRole;
import com.blt.govdata.service.ITSysUserRoleService;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户分配角色管理前端控制器
 * </p>
 * @author AustinGJ123
 * @since 2018-03-26
 */
@RestController
@RequestMapping("/tSysUserRole")
//todo 将所有的XXX修改为真实值
public class TSysUserRoleController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ITSysUserRoleService service;

    /**
     * 给用户配置角色（批量提交）
     */
    @RequestMapping(value = "/confUserRole",params = {"uId"})

    public Map<String, Object> confUserRole(Integer uId,String rId) {
        /**
         * 先物理删除旧的分配关系
         * */
        this.service.delete(BeanUtils.instantiate(EntityWrapper.class).where("u_id = {0}",uId));
        /**
         * 再插入新的分配关系
         * */
        if(StringUtils.isBlank(rId)) return ImmutableMap.of("status", "true", "data", true);
        List<String> rIds = Arrays.asList(rId.split(","));
        rIds.stream().forEach(e->{
            TSysUserRole sur = BeanUtils.instantiate(TSysUserRole.class);
            sur.setrId(Integer.valueOf(e));
            sur.setuId(uId);
            this.service.insert(sur);
        });
        logger.info("给用户配置角色");
        return ImmutableMap.of("status", "true", "data", true);
    }

}
